home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / UTIL / Acme Filters 2.3.2 folder.sit / Acme Filters 2.3.2 folder / Acme Filters 2.3.2 / Acme Filters オ / Sources / Touch.c < prev    next >
Text File  |  1996-02-03  |  8KB  |  299 lines

  1. /************ I N C L U D E   F I L E S *******************************************************/
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. #include "Acme Filters.h"
  7. #include "Touch.h"
  8.  
  9.  
  10.  
  11. /**********************************************************************************************
  12.  * Function Name:  Touch
  13.  *
  14.  * Description:    Set a file's creation and modification dates/times.
  15.  *
  16.  * Parameters:     iFile - file spec of a file.
  17.  *
  18.  * Return Value:   None.
  19.  **********************************************************************************************/
  20. void    Touch( FSSpec *iFile, unsigned long created, unsigned long modified )
  21. {
  22.     HFileParam        iInfo;
  23.     unsigned long    secs;
  24.     DateTimeRec        dt;
  25.     
  26.     gCancelReq |= Cancel();
  27.     ScrollIcon();
  28.     
  29.     // get the file's information
  30.     iInfo.ioNamePtr   = iFile->name;
  31.     iInfo.ioVRefNum   = iFile->vRefNum;
  32.     iInfo.ioDirID     = iFile->parID;
  33.     iInfo.ioFDirIndex = 0;
  34.     PBHGetFInfo( (HParmBlkPtr)&iInfo, false );
  35.     
  36.     // get the number of seconds since 1-Jan-1904
  37.     GetDateTime( &secs );
  38.     
  39.     Secs2Date( secs, &dt );
  40.     dt.second = 0;
  41.     Date2Secs( &dt, &secs );
  42.     
  43.     // set the file's information
  44.     iInfo.ioNamePtr   = iFile->name;
  45.     iInfo.ioVRefNum   = iFile->vRefNum;
  46.     iInfo.ioDirID     = iFile->parID;
  47.     iInfo.ioFDirIndex = 0;
  48.     iInfo.ioFlMdDat = (long)secs;
  49.     if ( created && modified )
  50.     {
  51.         iInfo.ioFlCrDat = (long)created;
  52.         iInfo.ioFlMdDat = (long)modified;
  53.     }
  54.     PBHSetFInfo( (HParmBlkPtr)&iInfo, false );
  55.     PBFlushFile( (ParmBlkPtr)&iInfo, false );
  56.     
  57.     TickleParentDir( iFile );
  58. }
  59.  
  60.  
  61.  
  62. /**********************************************************************************************
  63.  * Function Name:  PickDateTime
  64.  *
  65.  * Description:    View or specify a file's creation and modification dates/times.
  66.  *
  67.  * Parameters:     iFile - file spec of a file.
  68.  *                 created - the file's creation date.
  69.  *                 modified - the file's modification date.
  70.  *
  71.  * Return Value:   noErr if ok, error code otherwise.
  72.  **********************************************************************************************/
  73. OSErr    PickDateTime( FSSpec *iFile, unsigned long *created, unsigned long *modified )
  74. {
  75.     OSErr            err = noErr;
  76.     DialogPtr        picker;
  77.     WindowPtr        saveMe;
  78.     Boolean            done = false;
  79.     short            item;
  80.     ModalFilterUPP    filter = NewModalFilterProc( PickDateTimeDlgFltr );
  81.     
  82.     HFileParam        iInfo;
  83.     DateTimeRec        dt;
  84.     short            iType;
  85.     Handle            iHandle;
  86.     Rect            iRect;
  87.     Str255            str;
  88.     
  89.     if ( picker = GetNewDialog( kDateTimeDlog, nil, (WindowPtr)-1L ) )
  90.     {
  91.         // stuff the creation and modification dates into the two TE fields
  92.         // get the file's information
  93.         iInfo.ioNamePtr   = iFile->name;
  94.         iInfo.ioVRefNum   = iFile->vRefNum;
  95.         iInfo.ioDirID     = iFile->parID;
  96.         iInfo.ioFDirIndex = 0;
  97.         PBHGetFInfo( (HParmBlkPtr)&iInfo, false );
  98.         
  99.         DateToString( iInfo.ioFlCrDat, str );
  100.         GetDItem( picker, kCreated, &iType, &iHandle, &iRect );
  101.         SetIText( iHandle, str );
  102.         
  103.         DateToString( iInfo.ioFlMdDat, str );
  104.         GetDItem( picker, kModified, &iType, &iHandle, &iRect );
  105.         SetIText( iHandle, str );
  106.         SelIText( picker, kCreated, 0, kMaxDateTime );
  107.         
  108.         GetPort( &saveMe );
  109.         SetPort( picker );
  110.         ShowWindow( picker );
  111.         
  112.         while ( !done )
  113.         {
  114.             ModalDialog( filter, &item );
  115.             switch( item )
  116.             {
  117.                 case -1:            // -1 + 1 =  0, ok
  118.                 case -2:            // -2 + 1 = -1, cancel
  119.                     err = item + 1;
  120.                     done = true;
  121.                     break;
  122.                 
  123.                 case kCreated:
  124.                 case kModified:
  125.                     GetDItem( picker, item, &iType, &iHandle, &iRect );
  126.                     GetIText( iHandle, str );
  127.                     if ( str[0] > kMaxDateTime )
  128.                     {
  129.                         str[0] = kMaxDateTime;
  130.                         SetIText( iHandle, str );
  131.                         SysBeep( 1 );
  132.                     }
  133.                     break;
  134.             }
  135.         }
  136.         
  137.         if ( !err )
  138.         {
  139.             // retrieve the creation and modification dates from the two TE fields
  140.             for ( item = kCreated; item <= kModified; item++ )
  141.             {
  142.                 GetDItem( picker, item, &iType, &iHandle, &iRect );
  143.                 memset( (char *)str, kNil, 256 );
  144.                 GetIText( iHandle, str );
  145.                 sscanf( (char *)&str[1], kDateTimeFormat,
  146.                     &dt.month, &dt.day, &dt.year, &dt.hour, &dt.minute, &dt.second );
  147.                 if ( item == kCreated ) Date2Secs( &dt, created );
  148.                 if ( item == kModified ) Date2Secs( &dt, modified );
  149.             }
  150.         }
  151.         
  152.         SetPort( saveMe );    
  153.         DisposDialog( picker );
  154.     }
  155.     
  156.     return ( err );
  157. }
  158.  
  159.  
  160.  
  161. /**********************************************************************************************
  162.  * Function Name:  PickDateTimeDlgFltr
  163.  *
  164.  * Description:    Handle mouse and keystrokes.
  165.  *
  166.  * Parameters:     dialog - the dialog being filtered.
  167.  *                 event - the event record being processed.
  168.  *                 item - the item in which the event occured.
  169.  *
  170.  * Return Value:   true - ModalDialog returns immediately and without handling the event.
  171.  *                 false - ModalDialog handles the event normally.
  172.  **********************************************************************************************/
  173. pascal Boolean    PickDateTimeDlgFltr( DialogPtr dialog, EventRecord *event, short *item )
  174. {
  175.     char    c;
  176.     Boolean    result = false;
  177.     
  178.     *item = 0;
  179.     if ( (event->what == keyDown) || (event->what == autoKey) )
  180.     {
  181.         c = event->message & charCodeMask;
  182.         
  183.         if ( (c == kEnter) || (c == kReturn) )
  184.         {
  185.             if ( DateTimeOK( dialog, kCreated ) && DateTimeOK( dialog, kModified ) )
  186.                 *item = -1;
  187.             result = true;
  188.         }
  189.         
  190.         if ( (c == kEscape) || ((event->modifiers & cmdKey) && (c == kPeriod)) )
  191.         {
  192.             *item = -2;
  193.             result = true;
  194.         }
  195.     }
  196.     
  197.     return ( result );
  198. }
  199.  
  200.  
  201.  
  202. /**********************************************************************************************
  203.  * Function Name:  DateTimeOK
  204.  *
  205.  * Description:    Validate the numbers in the date and time.
  206.  *
  207.  * Parameters:     dialog - a dialog.
  208.  *                 item - the text edit item to check.
  209.  *
  210.  * Return Value:   true if ok, false otherwise.
  211.  **********************************************************************************************/
  212. Boolean    DateTimeOK( DialogPtr dialog, short item )
  213. {
  214.     short        iType;
  215.     Handle        iHandle;
  216.     Rect        iRect;
  217.     Str255        str;
  218.     DateTimeRec    dt;
  219.     Boolean        result = true;
  220.     
  221.     GetDItem( dialog, item, &iType, &iHandle, &iRect );
  222.     memset( (char *)str, kNil, 256 );
  223.     GetIText( iHandle, str );
  224.     
  225.     if ( sscanf( (char *)&str[1], kDateTimeFormat,
  226.         &dt.month, &dt.day, &dt.year, &dt.hour, &dt.minute, &dt.second ) != 6 )    result = false;
  227.     
  228.     if (   (dt.month < 1) || (dt.month > 12) )                result = false;
  229.     if (     (dt.day < 1) || (dt.day > DaysInMonth( dt )) )    result = false;
  230.     if ( (dt.year < 1904) || (dt.year > 2040) )                result = false;
  231.     if (    (dt.hour < 0) || (dt.hour > 23) )                result = false;
  232.     if (  (dt.minute < 0) || (dt.minute > 59) )                result = false;
  233.     if (  (dt.second < 0) || (dt.second > 59) )                result = false;
  234.     
  235.     return ( result );
  236. }
  237.  
  238.  
  239.  
  240. /**********************************************************************************************
  241.  * Function Name:  DateToString
  242.  *
  243.  * Description:    Create a MM/DD/YYYY HH:MM:SS formatted string.
  244.  *
  245.  * Parameters:     secs - the date in seconds since 01-Jan-1904.
  246.  *                 date - the date after formatting.
  247.  *
  248.  * Return Value:   None.
  249.  **********************************************************************************************/
  250. void    DateToString( unsigned long secs, Str255 date )
  251. {
  252.     DateTimeRec    dt;
  253.     Str255        temp;
  254.     
  255.     Secs2Date( secs, &dt );
  256.     date[0] = 0;
  257.     
  258.     // format date
  259.     if ( dt.month < 10) pstrcat( date, kLeadingZero );
  260.     NumToString( dt.month, temp );
  261.     pstrcat( date, temp );
  262.     pstrcat( date, kSlash );
  263.     if ( dt.day < 10) pstrcat( date, kLeadingZero );
  264.     NumToString( dt.day, temp );
  265.     pstrcat( date, temp );
  266.     pstrcat( date, kSlash );
  267.     NumToString( dt.year, temp );
  268.     pstrcat( date, temp );
  269.     
  270.     pstrcat( date, kTwoSpaces );
  271.     
  272.     // format time
  273.     if ( dt.hour < 10) pstrcat( date, kLeadingZero );
  274.     NumToString( dt.hour, temp );
  275.     pstrcat( date, temp );
  276.     pstrcat( date, kColon );
  277.     if ( dt.minute < 10) pstrcat( date, kLeadingZero );
  278.     NumToString( dt.minute, temp );
  279.     pstrcat( date, temp );
  280.     pstrcat( date, kColon );
  281.     if ( dt.second < 10) pstrcat( date, kLeadingZero );
  282.     NumToString( dt.second, temp );
  283.     pstrcat( date, temp );
  284. }
  285.  
  286.  
  287.  
  288. short    DaysInMonth( DateTimeRec dt )
  289. {
  290.     static short    dpm[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  291.     
  292.     return ( (LeapYear( dt ) && (dt.month == 2)) ? 29 : dpm[dt.month-1] );
  293. }
  294.  
  295. Boolean    LeapYear( DateTimeRec dt )
  296. {
  297.     return ( (!(dt.year % 4) && (dt.year % 100)) || !(dt.year % 400) ? true : false );
  298. }
  299.